class Dui {
    constructor(arr) {
        this.arr = arr || []
        this.arr.unshift('-')
        this.count = 1
    }
    inset(num) {
        this.arr.push(num)
        this.count = this.arr.length - 1
        while (this.count >= 1 && this.arr[this.count] > this.arr[~~(this.count / 2)]) {
            [this.arr[this.count], this.arr[~~(this.count / 2)]] = [this.arr[~~(this.count / 2)], this.arr[this.count]]
            this.count = ~~(this.count / 2)
        }
    }
    down(){
       [this.arr[this.arr.length-1],this.arr[1]]=[this.arr[1],this.arr[this.arr.length-1]]
       this.arr.pop()
       let i=1
       while(2*i<this.arr.length-1){
           if(this.arr[2*i]>this.arr[2*i+1]){
               [this.arr[i],this.arr[2*i]]=[this.arr[2*i],this.arr[i]]
               i=2*i
           }else{
               [this.arr[i],this.arr[2*i+1]]=[this.arr[2*i+1],this.arr[i]]
               i=2*i+1
           }
       }
    }

   sort(){
       let newa=[]
       let len=this.arr.length-1
       for(let i=0;i<len;i++){
           newa.push(this.arr[1])
           this.down()
       }
       return newa
   }
}


let a = new Dui()
for (let i = 1; i <=20; i++) {
    let n = ~~(Math.random() * 20)
    a.inset(n)
}
console.log(a.arr)

let r=a.sort()
console.log(r)